package com.mobiporter.adias.tags;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.jsp.JspException;

import com.mobiporter.adias.constants.RequestScopeObjects;
import com.mobiporter.adias.tags.model.SelectItem;
import com.mobiporter.adias.tags.utils.HTMLTagGenerator;
import com.mobiporter.adias.tags.utils.LinkGenerator;

public class SelectBox extends MobiTag
{
	
	
	private static final long serialVersionUID = -2832072693627153907L;
	private String selectedValue ;
	private List<SelectItem> items ;
	private String selectorUrl ; 
	private String page ;
	
	
	

	public String getPage() {
		return page;
	}



	public void setPage(String page) {
		this.page = page;
	}



	public String getSelectedValue() {
		return selectedValue;
	}



	public void setSelectedValue(String selectedValue) {
		this.selectedValue = selectedValue;
	}



	public List<SelectItem> getItems() {
		return items;
	}



	public void setItems(List<SelectItem> items) {
		this.items = items;
	}



	public String getSelectorUrl() {
		return selectorUrl;
	}



	public void setSelectorUrl(String selectorUrl) {
		this.selectorUrl = selectorUrl;
	}



	@SuppressWarnings("unchecked")
	@Override
	public int doEndTag() throws JspException 
	{
		setValue();
		if (false)
		{
			String selectBoxHTML = HTMLTagGenerator.getSelectBox(items, getId(), getSelectedValue());
			writeOut(selectBoxHTML);
		}
		else
		{
			if (items.size() > 4)
			{
				String label = items.get(0).getKey() ;
				String value = items.get(0).getValue() ;
				for (SelectItem item : items)
				{
					if (item.getValue().equals(getSelectedValue())) 
					{
						label = item.getKey() ;
						value = item.getValue() ;
					}
				}
				
				getSelectItemsList().remove(getId());
				getSelectItemsList().put(getId(), items);
				LinkGenerator linkGenerator = new LinkGenerator(getSelectorUrl()) ;
				linkGenerator.addRequestParam("toChange", getId());
				
				List<SelectItem> previousParams = new ArrayList<SelectItem>() ;
				for (Map.Entry<String, String[]> param : (Set<Map.Entry<String, String[]>>)pageContext.getRequest().getParameterMap().entrySet())
				{
					if (!param.getKey().equals(getId()))
					{
						SelectItem item = new SelectItem() ;
						item.setKey(param.getKey());
						item.setValue(param.getValue()[0]);
						previousParams.add(item);
					}
				}
				pageContext.getSession().setAttribute(RequestScopeObjects.PREVIOUS_PAGE_PARAMS, previousParams);
				try 
				{
					linkGenerator.addRequestParam("backLink", URLEncoder.encode(getPage(),"UTF-8" ));
				} 
				catch (UnsupportedEncodingException e) 
				{
					
				}
				String changeLink = HTMLTagGenerator.getLink("change_link", "change",linkGenerator.getUrl());
				
				String html = label + " ("+changeLink +")" + HTMLTagGenerator.getHiddenField(getId(),label,value) ;
				
				writeOut(html);
			}
			else
			{
				String html = "" ;
				for (SelectItem item : items)
				{
					html+= HTMLTagGenerator.getRadioButton(getId(),item.getKey(),item.getValue()) 
							+ HTMLTagGenerator.getBreakLine();
				}
				writeOut(html);
			}
		}
		items = null ;
		return EVAL_PAGE ;
	}
	
	

	@Override
	public void release() {
		items = null ;
	}



	@Override
	public int doStartTag() throws JspException 
	{
		if (items == null)
			items = new ArrayList<SelectItem>() ;
		pageContext.getRequest().setAttribute(RequestScopeObjects.SELECT_BOX_VALUES, items);
		
		return EVAL_BODY_INCLUDE ;
	}

	@Override
	public void setValueToControl(String value) 
	{
		this.selectedValue = value ;
	}
	
}
